﻿using System;
using System.IO;
using Microsoft.Xna.Framework.Audio;
using System.Threading;

namespace Morse
{
    public class SoundTask
    {
        private bool _stop = false;
        public void Play(string code)
        {
            _stop = false;
            int Dit = MorseCode.Dit, Dah = MorseCode.Dah;
            ThreadPool.QueueUserWorkItem(_ =>
            {
                var si = Sound.GetSount(5);
                var steps = MorseEncode.GetPlaySteps(code);
                foreach (var step in steps)
                {
                    if (_stop)
                        break;
                    if (step.Play)
                        si.Play();
                    else
                        si.Pause();
                    Thread.Sleep(step.Time);
                }
                si.Stop();
                si.Dispose();
            });
        }

        public void Stop()
        {
            _stop = true;
        }
    }
}
